home *** CD-ROM | disk | FTP | other *** search
- Path: grimsel.zurich.ibm.com!usenet
- From: wgk@zurich.ibm.com (Keith Whittingham)
- Newsgroups: comp.lang.c++
- Subject: Re: doublelinked lists
- Date: 3 Apr 1996 09:35:28 GMT
- Organization: IBM Research, ZRH
- Message-ID: <4jtgp0$34f@grimsel.zurich.ibm.com>
- References: <4jrs9g$1pl@news-e2c.gnn.com>
- Reply-To: wgk@zurich.ibm.com
- NNTP-Posting-Host: pine.zurich.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.00
-
- In <4jrs9g$1pl@news-e2c.gnn.com>, PLayton@gnn.com (Pam Layton) writes:
- >I need some help. I have written a program for my C++ class. It
- >contains a struct for name information, a stuct for address
- >information. Then 3 classes, 1 for the name info, 1 for address
- >info, and 1 for combining the two. This creates a nice little
- >address list. I have the information stored in an array. Now, I
- >need to modify this program to use a double linked list instead of
- >an array and be able to insert the information into the proper
- >spot. Alphabetically. Does anyone know the best way I can go
-
- I don't know what compiler you're using but have a hunt around for
- collection classes - all the hard work's been done for you. If you
- don't have these or don't want to use them then no problem, writing
- your owns not that big a deal.
-
- No you don't have to throw away your work - that's what C++ is all
- about. Write a linked list class and test it then derive your
- classes from the "element" class in your linked list...
-
- class Element // The elements of the linked list
- {
- public:
- virtual void Print(stream &s) const = 0;
- virtual ~Element() {};
-
- private:
- Element *Nic; // Next in chain (when this is member of list)
- };
-
- class SortedList: // The sorted linked list
- public Element // Derived from Element, not really needed but nice...
- {
- public:
- SortedList() { Fic = 0; }
- virtual ~SortedList();
- void Insert(Elemet &e);
- virtual void Print(stream &s) const;
-
- private:
- Element *Fic; // First in chain
- };
-
- I've included a method (pure virtual) called print in the element which
- makes Element an Abstract Base Class (forcing Sorted list to define this
- so you can print the list).
-
- Deriving SortedList from Element allows you to have a list of lists!
-
- Note the list is called SortedList rather than DoubleLinkedList so that
- the implmentation is hidden - it is of little interest to the user of
- the class. Would single linked list be easier to implement (and so
- less bugs).
-
- I've included an Insert method to insert a member in the list, you
- probably also need Find() and Delete() methods. You also need to
- worry about ownership of the elements: when I destruct a list do
- I destruct the members?
-
- The virtual destructers are a good idea on any function that has
- virtual methods
-
- Once tested and working derive your classes from Element:
-
- class Person:
- public Element
- {
- public:
- Name name;
- Address address;
- };
-
-
- class Name:
- public Element
- {
- ...
-
- And Bob's your uncle - you might even have his address! You're code then
- looks like this...
-
- main(int argc, char *argv[])
- {
- SortedList AddressBook;
- Person *Me = new Person("Keith", "Getysburg");
- AddressBook.Insert(Me);
- ...
- }
-
- I feel like a new Person.
-
- Keith
-
-
-
-
-